from docx import Document
import re

def preprocess(docx_path):
    """
    预处理 DOCX 文件，提取文本和简单的元数据
    """
    try:
        document = Document(docx_path)
        paragraphs = [para.text for para in document.paragraphs if para.text.strip() != ""]
        text = "\n".join(paragraphs)
    except Exception as e:
        print(f"处理文件 {docx_path} 时出现错误: {e}")
        return None, ""
    
    # 假设标准号格式 xxxx-xxxx 和发布日期格式 YYYY-MM-DD
    metadata = {
        "标准号": re.search(r'\d{4}-\d{4}', text).group(0) if re.search(r'\d{4}-\d{4}', text) else None,
        "发布日期": re.search(r'(\d{4}-\d{2}-\d{2})', text).group(0) if re.search(r'(\d{4}-\d{2}-\d{2})', text) else None,
        "段落数": len(document.paragraphs)
    }
    
    # 标准化文本，去除多余空白字符
    text = re.sub(r'[\s\u3000]+', ' ', text)
    return metadata, text